package sy.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.db.mysql.model.JKGL;
import sy.db.mysql.model.KsTm;
import sy.db.mysql.model.QDGL;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.SessionInfo;
import sy.services.JKGLServiceI;
import sy.services.QDGLServiceI;
import sy.util.ConfigUtil;

/**
 *  @author zxl
 */
@Controller
@RequestMapping("/qdglController")
public class QDGLController extends BaseController {
	@Autowired
	public QDGLServiceI qDGLService;
	@Autowired
	public JKGLServiceI jKGLService;
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return qDGLService.findbyByGrid(param);
	}
	
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(QDGL t ,String IP,String QDMC,String KEY, String YHM,String MIMA,String QXGL,String LEIBIE,String ZHUANGTAI,HttpSession session,NativeWebRequest request) {
		t.setLEIBIE(LEIBIE);
		t.setLOGUSERNAME(YHM);
		t.setQXGL(QXGL);
		t.setPASSWORD(MIMA);
		t.setZHUANGTAI(ZHUANGTAI);
		t.setOPENKEY(KEY);
		t.setIP(IP);
		t.setQDMC(QDMC);
		Json j = new Json();
		try {
			qDGLService.insert(t);
			j.setSuccess(true);
			j.setMsg("��ӳɹ���");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(QDGL t ,String IP,String QDMC,String KEY,Long ID,String YHM,String MIMA,String QXGL,String LEIBIE,String ZHUANGTAI,HttpSession session,NativeWebRequest request) {
		t.setID(ID);
		t.setLEIBIE(LEIBIE);
		t.setPASSWORD(MIMA);
		t.setQXGL(QXGL);
		t.setLOGUSERNAME(YHM);
		t.setZHUANGTAI(ZHUANGTAI);
		t.setOPENKEY(KEY);
		t.setIP(IP);
		t.setQDMC(QDMC);
		Json j = new Json();
		try {
			qDGLService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("��ӳɹ���");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			qDGLService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("��ӳɹ���");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage.shtml")
	public String viewPage(String id ,HttpServletRequest request) {
		QDGL qDGL = this.qDGLService.selectByPrimaryKey(id);
		request.setAttribute("qDGL", qDGL);
		return "/qDGL/qDGLView";
	}
	@RequestMapping("/manager.shtml")
	public String managerlist() {
		return "/qdgl/qdgl_list";
	}
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		return "/qdgl/qdgl_Add";
	}
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,String id) {
		QDGL qDGL = this.qDGLService.selectByPrimaryKey(id);
		request.setAttribute("qDGL", qDGL);
		return "/qdgl/qdgl_edit";
	}
	@RequestMapping("/qxfpPage")
	public String qxfpPage(HttpServletRequest request,String id) {
		 List list=jKGLService.qxfpselect();
		 System.out.println(list.toString());
		 request.setAttribute("list", list);
		 request.setAttribute("ID", id);
		return "/qdgl/qdgl_qxfp";
	}
	@RequestMapping("/qxfpPage.shtml")
	public String qxfpPage() {
		return "/qdgl/qxfp";
	}
	@RequestMapping("/kt_gl.shtml")
	public String qxfp_kt_gb() {
		return "/qdgl/qxfp_kt_gb";
	}
	/**
	 * 权限分配
	 * @param info
	 * @param ID
	 * @param t
	 * @param map
	 * @return
	 */
	@RequestMapping("/qxfp.shtml")
	@ResponseBody
	public Json qxfp(String[] info,Long ID,QDGL t) {
		List alist=new ArrayList<>();
		List blist=new ArrayList<>();
		List lt=new ArrayList<>();
		String[] fg;
		List<Map> list=qDGLService.qxck(ID);
		if(list.get(0)!=null){
			for(int i=0;i<list.size();i++){
				alist.add(list.get(i).get("QXGL"));
			}
			String sa=alist.toString();
			fg=sa.toString().replace("[", "").toString().replace("]", "").toString().split(",");
			for(int j=0;j<info.length;j++){
				lt.add(info[j]);
			}
			System.out.println(blist.toString().replace("[", "").toString().replace("]", "").toString());
			for(int i=0;i<=fg.length-1;i++){
				lt.add(fg[i].toString());		
			}
		}else{
			String sa=alist.toString();
			fg=sa.toString().replace("[", "").toString().replace("]", "").toString().split(",");
			for(int j=0;j<info.length;j++){
				blist.add(info[j]);
			}
			for(int j=0;j<blist.size();j++){
				lt.add(blist.get(j).toString());
			}
		}
		Json j = new Json();
		Set<String> linkedHashSet = new LinkedHashSet<String>(lt);
		List<Map> list2=qDGLService.qxck(ID);		
		t.setID(ID);
		t.setQXGL(linkedHashSet.toString().replace("[", "").toString().replace("]", "").toString().replace(" ","").toString());
		try {
		qDGLService.updateByPrimaryKeySelective(t);
		j.setSuccess(true);
		j.setMsg("��ӳɹ���");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
}
